package dialog;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import model.ModelCity;
import table.TabModCity;
import control.CtrConsultCity;
import dao.City;

public class DlgConsultCity extends JDialog {
	private JPanel contentPanel;
	
	private JPanel pnlNorth;
	private JTextField txtFilter;
	private JButton btnSearch;
	
	private JScrollPane scpCenter;
	private JTable tblCity;
	private ArrayList<City> colCity = new ArrayList<City>();
	private TabModCity modelTable;
	
	private JPanel pnlSouth;
	private JButton btnConfirm;
	private JButton btnCancel;
	
	private CtrConsultCity control;
	private ModelCity model;
	
	public DlgConsultCity(ArrayList<City> colCity) {
		this.setTitle("Selecionar Cidade");
		this.setSize(600,400);

		this.control = new CtrConsultCity(this);
		
		this.add(getContentPanel());
		
		this.colCity = colCity;
		tblCity.grabFocus();		
		
		this.setLocationRelativeTo(null);
		
		this.setModal(true);
		this.setVisible(true);
	}
	
	private JPanel getContentPanel(){
		if(contentPanel == null){
			contentPanel = new JPanel(new BorderLayout());	
			contentPanel.add("North",getPnlNorth());
			contentPanel.add("Center",getScpCenter());
			contentPanel.add("South",getPnlSouth());
		}
		return contentPanel;
	}
	
	private JPanel getPnlNorth() {
		if(pnlNorth == null){			
			pnlNorth = new JPanel(new FlowLayout(FlowLayout.LEFT));
			pnlNorth.add(new JLabel("Nome:"));
			pnlNorth.add(getTxtFilter());
			pnlNorth.add(getBtnSearch());
		}
		return pnlNorth;
	}

	private JTextField getTxtFilter() {
		if(txtFilter == null){			
			txtFilter = new JTextField(12);
			txtFilter.setName("txtFilter");
			txtFilter.addKeyListener(control);
		}
		return txtFilter;
	}

	private JButton getBtnSearch() {
		if(btnSearch == null){			
			btnSearch = new JButton("Pesquisar", new ImageIcon(ClassLoader.getSystemResource("img/icon_search.PNG")));
			btnSearch.setMnemonic('P');
			btnSearch.setName("btnSearch");
			btnSearch.addActionListener(control);
		}
		return btnSearch;
	}

	private JPanel getPnlSouth() {
		if(pnlSouth == null){			
			pnlSouth = new JPanel(new FlowLayout(FlowLayout.RIGHT));	
			pnlSouth.add(getBtnConfirm());
			pnlSouth.add(getBtnCancel());
		}
		return pnlSouth;
	}

	private JButton getBtnConfirm() {
		if(btnConfirm == null){			
			btnConfirm = new JButton("Confirmar", new ImageIcon(ClassLoader.getSystemResource("img/icon_ok.PNG")));	
			btnConfirm.setName("btnConfirm");
			btnConfirm.setMnemonic('F');
			btnConfirm.addActionListener(control);
		}
		return btnConfirm;
	}

	private JButton getBtnCancel() {
		if(btnCancel == null){			
			btnCancel = new JButton("Cancelar", new ImageIcon(ClassLoader.getSystemResource("img/icon_canc.PNG")));
			btnCancel.setName("btnCancel");
			btnCancel.setMnemonic('C');
			btnCancel.addActionListener(control);
		}
		return btnCancel;
	}
	
	public JTable getTblCity() {
		if(tblCity == null){			
			modelTable = new TabModCity(colCity);	
			tblCity = new JTable(modelTable);
			tblCity.setName("tblCity");
			tblCity.addKeyListener(control);
			tblCity.getColumnModel().getColumn(0).setMinWidth(60);
			tblCity.getColumnModel().getColumn(0).setMaxWidth(60);
		}
		return tblCity;
	}
	
	public JScrollPane getScpCenter() {
		if(scpCenter == null){
			scpCenter = new JScrollPane(getTblCity());
		}
		
		return scpCenter;
	}
	
	public City getCity(){
		if(colCity != null){
			if (tblCity.getSelectedRow() >= 0){
				return colCity.get(tblCity.getSelectedRow());
			}
		}
		return null;
	}
	
	public void setText(String text){
		txtFilter.setText(text);
	}
	
	public String getText(){		
		return txtFilter.getText().trim();		
	}
	
	public int getSelectedRow(){
		return tblCity.getSelectedRow();
	}
	
	public void setNull(){
		this.colCity = null;
	}
		
	public void update(ArrayList<City> colCity) {
		if(colCity != null){
			this.colCity = colCity;
			modelTable.setColCity(colCity);
		}
	}
}